﻿/*===========================================================================
	TASK 3:                                        {Multidimentional Arrays}
	We are given a matrix of strings of size N x M. Sequences in the matrix
    we define as sets of several neighbor elements located on the same line,
    column or diagonal. Write a program that finds the longest sequence 
    of equal strings in the matrix. Example:
 
           ha fifi ho  hi                         s, qq, s
           fo  ha  hi  xx  =>ha, ha, ha          pp, pp, s =>s,s,s
          xxx  ho  ha  xx                        pp, qq, s 
=============================================================================*/

using System;

class LongestSequenceOfEqualsStrings
{
    static void Main()
    {
        string[,] matrix = 
        {
            { "ha", "fifi","ho","hi"},
            { "fo", "ha",  "hi","xx"},
            { "xxx","ho",  "ha","xx"},
        };

        //int[,] matrix = 
        //{
        //    {6, 6, 6, 1},
        //    { 6, 3, 3, 2},
        //    { 9, 6, 5, 6 },
        //};

        int length = 1;
        int maxLength = 1;
        int maxStartCol = 0;
        int maxStartRow = 0;
        int direction = int.MinValue;

        //Check for max sequence in left-right diagonals
        int n = matrix.GetLength(0);
        int m = matrix.GetLength(1);
        int startR = 0;
        int startC = 0;
        int row1 = 0;
        int col1 = 0;

        for (int i = 0; i < n + m - 3; i++)
        {
            row1 = 0;
            if (i < m - 1)
            {
                col1 = m - 2 - i;
                row1 = 0;
                startC = col1;
                startR = row1;
            }
            else
            {
                col1 = 0;
                row1 ++;
                startC = col1;
                startR = row1;
            }
            
            while (true)
            {
                if (row1 == n - 1 || col1 == m - 1)
                {
                    break;
                }
                if (matrix[row1, col1] == matrix[row1 + 1, col1 + 1])
                {
                    length++;
                    if (length > maxLength)
                    {
                        maxLength = length;
                        maxStartCol = startC;
                        maxStartRow = startR;
                        direction = 2;
                    }
                    col1++;
                    row1++;
                }
                else
                {
                    col1++;
                    row1++;
                    startC = col1;
                    startR = row1;
                }
            }
            length = 1;
        }
        
        switch (direction)
        {
            case 0:
                for (int col = maxStartCol; col < maxStartCol + maxLength; col++)
                {
                    Console.Write(matrix[maxStartRow, col] + " ");
                }
                break;
            case 1:
                for (int row = maxStartRow; row < maxStartRow + maxLength; row++)
                {
                    Console.WriteLine(matrix[row, maxStartCol]);
                }
                break;
            case 2:
                for (int i = 0; i < maxLength; i++)
                {
                    Console.Write(matrix[maxStartRow, maxStartCol] + " ");
                    maxStartRow++;
                    maxStartCol++;
                }
                break;
        }
    }
}